package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import utilClasses.ListaStanica;
import utilClasses.uStanica;

public class daoStanica {

	public static int kreirajStanicu(double gduzina, double gsirina,
			String naziv, int redni, int linija) throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c
				.prepareCall("{CALL Insert_stanica(?,?,?,?,?,?)}");

		cStmt.setDouble(2, gduzina);
		cStmt.setDouble(1, gsirina);
		cStmt.setString(3, naziv);
		cStmt.setInt(4, redni);
		cStmt.setInt(5, linija);
		cStmt.registerOutParameter(6, java.sql.Types.INTEGER);
		cStmt.executeUpdate();
		int lastId=-1;
		lastId=cStmt.getInt(6);
		System.out.println("Uspjesno unesena stanica!\n");
		cStmt.close();
		c.close();
		return lastId;
	}

	public static String pomjeriStanicu(int id, double GS, double GD)
			throws SQLException {
		String Rez = "false";
		Connection c = daoKonekcija.dajKonekciju();
		try {
			CallableStatement cStmt = c
					.prepareCall("{CALL pomakniStanicu(?,?,?)}");
			cStmt.setInt(1, id);
			cStmt.setDouble(2, GS);
			cStmt.setDouble(3, GD);
			cStmt.execute();
			Rez = "true";

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			c.close();
		}
		return Rez;
	}

	public static ListaStanica dajStanice(int id) throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
		ListaStanica rez = new ListaStanica();
		try {
			PreparedStatement stmt = c.prepareStatement("SELECT * FROM stanica WHERE linija=? order by redniBroj;");
			stmt.setInt(1,id);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				int idStanice = rs.getInt("idstanica");
				double gduzina = rs.getDouble("GDuzina");
				double gsirina = rs.getDouble("GSirina");
				String naziv = rs.getString("Naziv");
				int redni = rs.getInt("redniBroj");
				int linija =rs.getInt("linija");
				rez.getStanice().add(
						new uStanica(idStanice,gduzina, gsirina, naziv, redni,linija));
			}
			rs.close();
			c.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if (c != null)
				c.close();
			e.printStackTrace();
		}

		return rez;
	}

	static int dajidStanice(String naziv) throws SQLException {
		// OVO ne bi trebalo ovako!!!!
		Connection c = daoKonekcija.dajKonekciju();
		PreparedStatement ps = c
				.prepareStatement("SELECT idstanica FROM stanica WHERE Naziv=?");
		ps.setString(1, naziv);
		ResultSet rs = ps.executeQuery();
		if (!rs.next())
			return -1;
		int rez = rs.getInt(1);
		rs.close();
		c.close();
		return rez;

	}

	public static void brisiStanicu(String naziv) throws Exception {
		Connection c = daoKonekcija.dajKonekciju();
		int idStanice = dajidStanice(naziv);
		if (idStanice < 0)
			throw new Exception("Nema stanice sa tim ID-em!");
		CallableStatement cStmt = c.prepareCall("{CALL deleteStanica(?)}");
		cStmt.setInt(1, idStanice);
		cStmt.execute();
		System.out.println("Uspjesno");
		cStmt.close();
		c.close();
	}

	public static ListaStanica dajImenaStanica() throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
		ListaStanica rez = new ListaStanica();
		String iskaz = "SELECT Naziv FROM stanica";
		try {
			Statement stmt = c.createStatement();
			ResultSet rs = stmt.executeQuery(iskaz);
			while (rs.next()) {
				String naziv = rs.getString(1);
				rez.getStanice().add(new uStanica(naziv));
			}
			rs.close();
			stmt.close();
			c.close();
			return rez;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if (c != null)
				c.close();
			e.printStackTrace();
		}
		return rez;

	}
}
